<?php
class CViUser
{
	static $current;
	public $client;
	function __construct()
	{
		require_once('3rdparty/nusoap/nusoap.php');
		$web_service=portal()->setting('web-service');
		
		$this->client = new nusoap_client((string)$web_service['url'], 'wsdl', '', '');
		$this->client->setCredentials  ($web_service["user"], $web_service["password"], "basic");
		$this->client->soap_defencoding = 'UTF-8';
		$this->client->decode_utf8 = false;
		$err = $this->client->getError();
		if ($err) 
		{
			//echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
		}
	/*	else
		{
				echo 'OK';
		}
		*/
	}
	function error()
	{	
		if(DEBUG)
		{
			$err = $this->client->getError();
			if ($err) 
			{ 
				echo 'have error';
				print_r($err);
			}		
		}
	}
	function is_exist_user_name($user_name)
	{
		
		$param = array ('userName'=>$user_name);
		$user = $this->client->call ('GetUser', array ('parameters'=>$param));	
		if ($user)
			return $user;		
		else
			return false;
	}
	function is_exist_email($email)
	{
		$param = array ('email'=>$email);
		$user = $this->client->call ('GetUserByEmail', $param);
		return $user;
	}
	function is_exist_mobile($email)
	{		
		return false;
	}
	function login($user_name, $password)
	{
		$param = array ('UserName'=>$user_name,'EncryptedPassword'=>$password,'Address'=>getRealIpAddr());		
		$user = $this->client->call('LogIn', array ('auth'=>$param));	
		$this->error();
		return $user;
	}
	function log_out()
	{
		$ticket=session('AuthenticationTicket');
		if (!empty($ticket))
		{
			$user = $this->client->call('LogOut', array ('ticket'=>$ticket));	
			$this->error();
			return $user;
		}
	}
	function encrypt_password($password)
	{
		$en_pass =$this->client->call('EncryptPassword',array('password'=>$password));			
		return $en_pass['EncryptPasswordResult'];
	}
	function register($data)
	{
		$result = $this->client->call('Register', array('user'=>$data));
		$this->error();
		return $result;
	}
	function active($userId)
	{	
		$result = $this->client->call('Active', array('userId'=>$userId));
		return $result;	
	}
	function update_info($userId,$new_info)
	{
		$result = $this->client->call('UpdateUserInfo', array('userId'=>$userId,'newInfo'=>$new_info));
		$this->error();
		print_r ($result);
		return $result;	
	}
	function change_pass($user_id,$current_password,$new_password)
	{
		$result = $this->client->call('ChangePassword', array('id'=>$user_id,'currentEncryptedPassword'=>$current_password,'newEncryptedPassword'=>$new_password));
		return $result;	
	}
	function forgot_pass()
	{
		$result = $this->client->call('UpdateUserInfo', array('userId'=>$userId,'newUserInfo'=>$new_info));
		return $result;	
	}
	/**
	 * Thay đổi số điện thoại
	 *
	 * @param long $user_id
	 * @param string $password
	 * @param string $new_mobile
	 * @return unknown
	 */
	function change_mobile($user_id,$password,$new_email)
	{
		$result = $this->client->call('ChangeMobile', array('userId'=>$user_id,'currentEncryptedPassword'=>$password,'newMobile'=>(string)$new_email));		
		return $result;	
	}
	function change_email($user_id,$password,$new_mobile)
	{
		$result = $this->client->call('ChangeEmail', array('userId'=>$user_id,'currentEncryptedPassword'=>$password,'newEmail'=>(string)$new_mobile));
		return $result;	
	}
}
function &viuser()
{
	if(!CViUser::$current)
	{
		CViUser::$current = new CViUser();
	}
	return CViUser::$current;
}
function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
?>